package com.frxuexi.park2209.mynews

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.frxuexi.base.entity.MyNewsBeanItem
import com.frxuexi.demo.databinding.ItemNewsBinding
import com.frxuexi.demo.news.NewsPageAdapter
import com.frxuexi.park2209.R
import com.frxuexi.park2209.databinding.ItemMynewsBinding

class MyMewsAdapter:PagingDataAdapter<MyNewsBeanItem, MyMewsAdapter.MyViewHolder>(diffutil) {
    companion object{
        val diffutil = object :ItemCallback<MyNewsBeanItem>(){
            override fun areItemsTheSame(
                oldItem: MyNewsBeanItem,
                newItem: MyNewsBeanItem
            ): Boolean {
                return oldItem.newsId==newItem.newsId
            }

            override fun areContentsTheSame(
                oldItem: MyNewsBeanItem,
                newItem: MyNewsBeanItem
            ): Boolean {
                return oldItem==newItem
            }
        }
    }


    class MyViewHolder(itemView:View):ViewHolder(itemView){
        val binding:ItemMynewsBinding?=DataBindingUtil.bind(itemView)
    }

    override fun onBindViewHolder(holder: MyMewsAdapter.MyViewHolder, position: Int) {
        holder.binding?.apply {
            newsTitle!!.text=getItem(position)?.newsTitle
            newsValue.text=getItem(position)?.newsValue
            newsTime.text=getItem(position)?.newsTime
        }
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): MyMewsAdapter.MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_mynews, parent, false)
        return MyMewsAdapter.MyViewHolder(view)
    }
}